<?php
class Admin_MedicineController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pDid = empty($p['d_id'])?'':Tool_Fnc::safe_string($p['d_id']);
        $pKeyname = empty($p['key_name'])?'':Tool_Fnc::safe_string($p['key_name']);
        $pMedicinedate = empty($p['medicine_date'])?'':Tool_Fnc::safe_string($p['medicine_date']);

        $tWhere = ' 1 ';

        if(!empty($pKeyname)){
            $tWhere .= ' and name like \'%'.$pKeyname.'\'';
        }

        if(!empty($pMedicinedate)){
            $tWhere .= ' and FROM_UNIXTIME(created,\'%Y-%m-%d\') = \''.$pMedicinedate.'\'';
        }
        $tMLMO = new MedicinelibraryModel;
        
        $tCount = $tMLMO->field('count(*) c')->where($tWhere)->fRow();
        //$tSql = 'select count(*) c from '.$tMLMO->table.' where '.$tWhere;
        $tCnt = empty($tCount['c'])?0:$tCount['c'];

        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);
        if(!empty($tCnt)){
            $tDatas = $tMLMO->field('*')->where($tWhere)->order(' id desc')->limit($tPage->limit())->fList();
        }

        if(!empty($pDid)){//编辑页面显示
            $tMLData = $tMLMO->field('*')->where('id = '.$pDid)->fRow();
            Tool_Fnc::ajaxMsg('',1,$tMLData);
        }

        $this->assign('pKeyName',$pKeyname);
        $this->assign('pMedicinedate',$pMedicinedate);
        $this->assign('tShow',$tPage->show());
        $this->assign('tDatas',$tDatas);
    }
    public function addAction(){
        $p = $_REQUEST;
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pName = empty($p['name'])?Tool_Fnc::ajaxMsg('药品名称不能为空'):Tool_Fnc::safe_string($p['name']);
            //$pImg = empty($p['img'])?Tool_Fnc::ajaxMsg('影像字节流不能为空'):Tool_Fnc::safe_string($p['img']);
            $pRemarks = empty($p['remarks'])?'':Tool_Fnc::safe_string($p['remarks']);

            $tTime = time();
            $tImg = '';

            if(empty($_FILES['img'])){Tool_Fnc::ajaxMsg('药品图片不能为空');}

            if(!empty($_FILES['img'])){
                $tImgurl = '/upload/medicine/'.date('Y').'/'.date('m').'/'.date('d').'/';  
                $tImgurl_small = '/upload/medicine/'.date('Y').'/'.date('m').'/'.date('d').'/small/';  
                $tDir = APPLICATION_PATH . '/public' . $tImgurl;
		        $tUpload = new Tool_Upload($_FILES['img'] , $tDir);		
                $tSavename = $tUpload->getSaveName();
                //$tUpload->createThumb('100','100',APPLICATION_PATH . '/public/' . $tImgurl_small);
		        $tExt = $tUpload->extension;
			    $tFile =  $tSavename . '.' . $tExt;
                $tRes = $tUpload->upload($tSavename);
                if($tRes == 1){
                    $tImg = $tImgurl . $tFile; 
                    $tData = array(
                        'name' => $pName,
                        'img' => $tImg,
                        'remarks' => $pRemarks,
                        'username' => $_SESSION['admin']['name'],
                        'created' => $tTime,
                    );
                    $tMLMO = new MedicinelibraryModel;
                    if(!$tMLMO->insert($tData)){
                        Tool_Fnc::ajaxMsg('上传失败');
                    }
                }else{
                        Tool_Fnc::ajaxMsg('图片上传失败');
                }
            }
                Tool_Fnc::ajaxMsg('添加成功',1);
        } 
    }
    public function editAction(){
        $p = $_REQUEST;
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pTid = empty($p['tId'])?Tool_Fnc::ajaxMsg('id不能为空'):Tool_Fnc::safe_string($p['tId']);
            $pName = empty($p['name'])?Tool_Fnc::ajaxMsg('药品名称不能为空'):Tool_Fnc::safe_string($p['name']);
            $pImgnew = empty($p['imgnew'])?'':Tool_Fnc::safe_string($p['imgnew']);
            $pRemarks = empty($p['remarks'])?'':Tool_Fnc::safe_string($p['remarks']);

            $tTime = time();
            $tImg = '';
            
            $tMLMO = new MedicinelibraryModel;

            if(!empty($_FILES['img'])){
                $tImgurl = '/upload/medicine/'.date('Y').'/'.date('m').'/'.date('d').'/';  
                $tImgurl_small = '/upload/medicine/'.date('Y').'/'.date('m').'/'.date('d').'/small/';  
                $tDir = APPLICATION_PATH . '/public' . $tImgurl;
		        $tUpload = new Tool_Upload($_FILES['img'] , $tDir);		
                $tSavename = $tUpload->getSaveName();
                //$tUpload->createThumb('100','100',APPLICATION_PATH . '/public/' . $tImgurl_small);
		        $tExt = $tUpload->extension;
			    $tFile =  $tSavename . '.' . $tExt;
                $tRes = $tUpload->upload($tSavename);
                if($tRes == 1){
                    $tImg = $tImgurl . $tFile; 
                    $tData = array(
                        'name' => $pName,
                        'img' => $tImg,
                        'remarks' => $pRemarks,
                        'username' => $_SESSION['admin']['name'],
                        'created' => $tTime,
                        'id' => $pTid,
                    );
                    if(!$tMLMO->update($tData)){
                        Tool_Fnc::ajaxMsg('修改失败');
                    }
                }else{
                    Tool_Fnc::ajaxMsg('图片上传失败');
                }
            }else{
                $tData = array(
                    'name' => $pName,
                    'img' => $pImgnew,
                    'remarks' => $pRemarks,
                    'username' => $_SESSION['admin']['name'],
                    'created' => $tTime,
                    'id' => $pTid,
                );
                if(!$tMLMO->update($tData)){
                    Tool_Fnc::ajaxMsg('修改失败');
                }
            }
            Tool_Fnc::ajaxMsg('修改成功',1);
        } 
    }
    public function deleteAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('id不能为空'):Tool_Fnc::safe_string($p['id']);

        $tMO = new MedicinelibraryModel;
        $tRow = $tMO->field('img')->where('id = '.$pId)->fRow();
        if(!empty($tRow['img'])){
            @unlink(APPLICATION_PATH . '/public' . $tRow['img']);
        }
        $tSql = 'delete from '.$tMO->table.' where id = '.$pId;
        if(!$tMO->exec($tSql)){
            Tool_Fnc::ajaxMsg('删除失败');
        }
        Tool_Fnc::ajaxMsg('删除成功',1);
    }
}
?>
